package common.utils.activity;

import zekezang.org.MainActivity;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class ActivityUtils {

	private static ActivityUtils activityTransitionUtils = new ActivityUtils();

	public static ActivityUtils getInstance() {
		return activityTransitionUtils;
	}

	public void leftIN_rightOUT_Transition(Activity activity, Context context, Class<?> targetClass) {

		Intent intent = new Intent(context, targetClass);
//		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		int i = ActivityQueue.list.indexOf(activity.getClass());
		if (i != ActivityQueue.list.size() - 1) {
			for (int k = i; k < ActivityQueue.list.size(); k++) {
				ActivityQueue.list.remove(k);
			}
		}
		// 屏避tab activity
		if (targetClass.getName().indexOf("Tab") != 0) {
			ActivityQueue.list.add(targetClass);
		}
		activity.startActivity(intent);
		activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
	}

	public void leftIN_rightOUT_Transition(Activity activity, Context context, Class<?> targetClass, String key,
			String value) {
		Intent intent = new Intent(context, targetClass);
		// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(key, value);

		int i = ActivityQueue.list.indexOf(activity.getClass());
		if (i < ActivityQueue.list.size() - 1 && i != -1) {
			for (int k = i + 1; k < ActivityQueue.list.size(); k++) {
				ActivityQueue.list.remove(k);
			}
		}
		// 屏避tab activity
		Log.i("zekezang", "targetClass.getName().indexOf(" + targetClass.getName().indexOf("Tab"));
		if (targetClass.getName().indexOf("Tab") != 0) {
			Log.i("zekezang", "targetClass:" + targetClass.getName());
			ActivityQueue.list.add(targetClass);
		}
		activity.startActivity(intent);
		activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
	}

	public void leftIN_rightOUT_close_Transition(Activity activity, Context context, Class<?> targetClass) {
		Intent intent = new Intent(context, targetClass);
		// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		activity.startActivity(intent);
		activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		activity.finish();
	}

	public void leftIN_rightOUT_close_Transition(Activity activity, Context context, Class<?> targetClass, String key,
			String value) {
		Intent intent = new Intent(context, targetClass);
		intent.putExtra(key, value);
		// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		activity.startActivity(intent);
		activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		activity.finish();
	}

	public void back_leftOUT_rightIN_Transition(Activity activity, Context context) {
		int i = ActivityQueue.list.indexOf(activity.getClass());
		if (ActivityQueue.list.size() > 0 && i > 0) {
			Intent intent = new Intent(context, ActivityQueue.list.get(i - 1));
			// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			activity.startActivity(intent);
			activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		} else if (i <= 0) {
			ActivityQueue.list.clear();
			ConfirmExit(activity, context);
		} else {
			activity.finish();
		}
	}

	public void to_home_Transition(Activity activity, Context context, Class<?> targetClass) {
		ActivityQueue.list.clear();
		ActivityQueue.list.add(targetClass);
		Intent intent = new Intent(context, targetClass);
		// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		activity.startActivity(intent);
		activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
	}

	public void forward_leftOUT_rightIN_Transition(Activity activity, Context context) {
		int i = ActivityQueue.list.indexOf(activity.getClass());
		Log.i("zekezang", "i:" + i);
		Log.i("zekezang", "ActivityQueue.list.size()" + ActivityQueue.list.size());
		if (ActivityQueue.list.size() > 0 && i < ActivityQueue.list.size() - 1) {
			Intent intent = new Intent(context, ActivityQueue.list.get(i + 1));
			// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			activity.startActivity(intent);
			activity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		}
	}

	public void exitClient(Context context) {
		// 关闭所有Activity
		for (int i = 0; i < ActivityQueue.activityList.size(); i++) {
			if (null != ActivityQueue.activityList.get(i)) {
				ActivityQueue.activityList.get(i).finish();
			}
		}
		ActivityQueue.list.clear();
		ActivityQueue.activityList.clear();
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		activityManager.killBackgroundProcesses("zekezang.org");
		System.exit(0);
	}

	public void ConfirmExit(Activity activity, final Context context) {// 退出确认
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle("退出");
		builder.setMessage("是否退出软件?");
		builder.setPositiveButton("是", new DialogInterface.OnClickListener() {// 退出按钮
					@Override
					public void onClick(DialogInterface dialog, int i) {
						exitClient(context);
					}
				});
		builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int i) {
				// 不退出不用执行任何操作
			}
		});
		builder.show();// 显示对话框
	}

	public void netSpeedSlowly(final Activity activity, final Context context) {// 退出确认
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle("故障");
		builder.setMessage("网络拥挤,主页将使用最后一次数据!");
		builder.setNegativeButton("是", new DialogInterface.OnClickListener() {// 退出按钮
					@Override
					public void onClick(DialogInterface dialog, int i) {
						ActivityUtils.getInstance().leftIN_rightOUT_Transition(activity, context, MainActivity.class,
								"dataStr", null);
					}
				});
		builder.show();// 显示对话框
	}

	public void noNetWork(final Activity activity, final Context context) {// 退出确认
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle("故障");
		builder.setMessage("网络没有连接!");
		builder.setNegativeButton("是", new DialogInterface.OnClickListener() {// 退出按钮
					@Override
					public void onClick(DialogInterface dialog, int i) {
						exitClient(context);
					}
				});
		builder.show();// 显示对话框
	}

	// 自由屏
	public void setScreenOrientation(Activity activity) {
		activity.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
	}

	// //豎屏
	public void setPortpaitScreenOrientation(Activity activity) {
		activity.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 豎屏
	}

	// 全屏
	public void setFullScreenOrientation(Activity activity) {
		Window window = activity.getWindow();
		window.requestFeature(Window.FEATURE_NO_TITLE);
		window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 豎屏
		// activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	/**
	 * 功能：全屏，自由屏
	 * 
	 * @param activity
	 */
	public void setFullScreen(Activity activity) {
		Window window = activity.getWindow();
		window.requestFeature(Window.FEATURE_NO_TITLE);
		window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public void setFullScreenHasTitle(Activity activity, String title) {
		Window window = activity.getWindow();
		if (null != title) {
			activity.setTitle(title);
		}
		window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	// 初始化版本，从mainfest里面读
	public void initVersion(Context context, TextView welcome_version) {
		try {
			PackageInfo packageInfo = context.getPackageManager().getPackageInfo("zekezang.org",
					PackageManager.GET_CONFIGURATIONS);
			welcome_version.setText(packageInfo.versionName);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private DisplayMetrics metrics = null;

	private void getMetrics(Activity ac) {
		metrics = new DisplayMetrics();
		ac.getWindowManager().getDefaultDisplay().getMetrics(metrics);
	}

	// 获取屏幕宽高
	public int[] getWidthHight(Activity ac) {
		getMetrics(ac);
		return new int[] { metrics.widthPixels, metrics.heightPixels };
	}

}
